package com.wyd.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.formLogin(form -> form
//                .loginPage("/login")
//                .permitAll()
//        );

        http.authorizeRequests().antMatchers("/**").permitAll();

//        http.authorizeRequests((authorize) -> authorize
////                .antMatchers("/css/**", "/index").permitAll()
////                .antMatchers("/user/**").hasRole("USER")
////                .antMatchers("/dict").permitAll()
////                .antMatchers("/dict/**").permitAll()
//                        .antMatchers("/**").permitAll()
//                        .antMatchers("**/dict/public/").permitAll()
//                        .antMatchers("**/dict/public/**").permitAll()
//                        .antMatchers("/**/public").permitAll()
//                        .antMatchers("/**/public/**").permitAll()
//                        .antMatchers("/**/dict/**").permitAll()
//                        .anyRequest().authenticated()
//        );
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        // swagger
        web.ignoring().antMatchers(
                "/v3/api-docs",
                "/swagger-resources/**",
                "/swagger-ui/**"
        );

        // web.ignoring().antMatchers("/**/public/**");
        // web.ignoring().mvcMatchers("/**/public**");
    }

    /**
     * 密码器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
